package com.xh3140.xwidget.dialog.view;

import android.content.Context;
import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.RippleDrawable;
import android.os.CountDownTimer;
import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;

import com.xh3140.xwidget.dialog.params.ButtonParams;

import java.util.Locale;

/**
 * 底部视图布局中的按钮视图
 */
@SuppressWarnings("unused")
public class ButtonView extends AppCompatTextView {
  private final static int[] STATES_ENABLED = {android.R.attr.state_enabled};
  private final static int[] STATES_DISABLED = {-android.R.attr.state_enabled};
  private final static int[][] STATES_ENABLE_ARRAY = {STATES_ENABLED, STATES_DISABLED};

  private final int mWhichButton;
  private CountDownTimerInner mCountDownTimer;

  private ButtonView(@NonNull Context context) {
    this(context, DialogInterface.BUTTON_POSITIVE);
  }

  public ButtonView(@NonNull Context context, int which) {
    super(context);
    if (which != DialogInterface.BUTTON_POSITIVE
            && which != DialogInterface.BUTTON_NEGATIVE
            && which != DialogInterface.BUTTON_NEUTRAL) {
      throw new IllegalArgumentException("Unknown button which.");
    }
    mWhichButton = which;
    final FooterView.LayoutParams params = new FooterView.LayoutParams(0, FooterView.LayoutParams.WRAP_CONTENT, 1f);
    params.gravity = Gravity.CENTER_VERTICAL;
    setLayoutParams(params);
  }

  public ButtonView configView(@NonNull final ButtonParams params) {
    setText(params.text);
    setTextSize(params.textSize);
    setEnabled(!params.disabled);
    setTextColor(new ColorStateList(STATES_ENABLE_ARRAY, new int[]{params.textColor, params.disabledTextColor}));
    setTypeface(getTypeface(), params.typeface);
    setGravity(params.gravity);
    setBackground(new RippleDrawable(ColorStateList.valueOf(params.pressedBackgroundColor),
            null, new ColorDrawable(params.backgroundColor)));
    if (params.padding != null) {
      setPaddingRelative(params.padding[0], params.padding[1], params.padding[2], params.padding[3]);
    }
    if (params.delay > 0) {
      if (mCountDownTimer != null) {
        mCountDownTimer.onFinish();
        mCountDownTimer.cancel();
      }
      mCountDownTimer = new CountDownTimerInner(params.delay, params.text, params.delayFormat);
    }
    setVisibility(params.text == null ? GONE : VISIBLE);
    if (params.callback != null) {
      params.callback.callback(this);
    }
    return this;
  }

  private class CountDownTimerInner extends CountDownTimer {
    private final CharSequence mText;
    private final String mFormat;

    public CountDownTimerInner(int delay, CharSequence text, String format) {
      super(delay * 1000L, 1000L);
      if (delay <= 0) {
        throw new IllegalArgumentException("delay <= 0.");
      }
      mText = text;
      mFormat = format;
      setEnabled(false);
    }

    @Override
    public void onTick(long millisUntilFinished) {
      setEnabled(false);
      final int countdown = (int) Math.ceil(1e-3 * millisUntilFinished);
      if (mFormat != null) {
        setText(String.format(Locale.getDefault(), mFormat, countdown));
      } else {
        setText(String.format(Locale.getDefault(), "%s(%ds)", mText, countdown));
      }
    }

    @Override
    public void onFinish() {
      setEnabled(true);
      setText(mText);
    }
  }

  @Override
  protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    if (mCountDownTimer != null) {
      mCountDownTimer.start();
    }
  }

  @Override
  protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    if (mCountDownTimer != null) {
      mCountDownTimer.onFinish();
      mCountDownTimer.cancel();
    }
  }

  public boolean isPositive() {
    return mWhichButton == DialogInterface.BUTTON_POSITIVE;
  }

  public boolean isNegative() {
    return mWhichButton == DialogInterface.BUTTON_NEGATIVE;
  }

  public boolean isNeutral() {
    return mWhichButton == DialogInterface.BUTTON_NEUTRAL;
  }
}